<html>
<head><meta charset="utf-8"><title>REPL: closure vs locals · t-compiler · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/index.html">t-compiler</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/REPL.3A.20closure.20vs.20locals.html">REPL: closure vs locals</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="182187702"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/REPL%3A%20closure%20vs%20locals/near/182187702" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/REPL.3A.20closure.20vs.20locals.html#182187702">(Nov 29 2019 at 15:55)</a>:</h4>
<blockquote>
<p><span class="user-mention silent" data-user-id="124288">oli</span> yes, that would be acceptable, I think, but would involve some more manipulation of the AST which seemed unnecessary. (that is, this seemed the simpler solution, at no noteworthy performance hit.)</p>
</blockquote>
<p>the advantage of the local variable is (I believe) that you can get rid of the closure entirely, and reuse <a href="https://doc.rust-lang.org/nightly/nightly-rustc/rustc/ty/struct.TyCtxt.html#method.entry_fn" target="_blank" title="https://doc.rust-lang.org/nightly/nightly-rustc/rustc/ty/struct.TyCtxt.html#method.entry_fn">https://doc.rust-lang.org/nightly/nightly-rustc/rustc/ty/struct.TyCtxt.html#method.entry_fn</a></p>



<a name="182188982"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/REPL%3A%20closure%20vs%20locals/near/182188982" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alexander Regueiro <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/REPL.3A.20closure.20vs.20locals.html#182188982">(Nov 29 2019 at 16:14)</a>:</h4>
<p>that's true, I think, but beyond the above benefit, the closure serves as a nice compartmentalisation of the "user fn", since there would be statements after it in <code>main</code> in any case (and in the future, possibly even before)</p>



<a name="182189180"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/REPL%3A%20closure%20vs%20locals/near/182189180" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/REPL.3A.20closure.20vs.20locals.html#182189180">(Nov 29 2019 at 16:18)</a>:</h4>
<p>oh, diagnostics again, right</p>



<a name="182189230"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/REPL%3A%20closure%20vs%20locals/near/182189230" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/REPL.3A.20closure.20vs.20locals.html#182189230">(Nov 29 2019 at 16:18)</a>:</h4>
<p>otherwise you may get errors/warnings about things you didn't write</p>



<a name="182189349"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/REPL%3A%20closure%20vs%20locals/near/182189349" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alexander Regueiro <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/REPL.3A.20closure.20vs.20locals.html#182189349">(Nov 29 2019 at 16:21)</a>:</h4>
<p>yup</p>



<a name="182189537"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/REPL%3A%20closure%20vs%20locals/near/182189537" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alexander Regueiro <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/REPL.3A.20closure.20vs.20locals.html#182189537">(Nov 29 2019 at 16:24)</a>:</h4>
<p>but the other point I was thinking of: with the closure, it's very straightforward for the miri evaluator to detect when the "user fn" is done with, and the locals' memory can be saved (and not any other locals in <code>main</code>, which shouldn't be saved).</p>



<a name="182189645"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/REPL%3A%20closure%20vs%20locals/near/182189645" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/REPL.3A.20closure.20vs.20locals.html#182189645">(Nov 29 2019 at 16:26)</a>:</h4>
<p>right</p>



<a name="182189780"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/REPL%3A%20closure%20vs%20locals/near/182189780" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alexander Regueiro <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/REPL.3A.20closure.20vs.20locals.html#182189780">(Nov 29 2019 at 16:29)</a>:</h4>
<p>cool, glad this is clear now.</p>



<a name="182189881"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/REPL%3A%20closure%20vs%20locals/near/182189881" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/REPL.3A.20closure.20vs.20locals.html#182189881">(Nov 29 2019 at 16:30)</a>:</h4>
<p>Thanks for explaining it</p>



<a name="182189982"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/REPL%3A%20closure%20vs%20locals/near/182189982" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alexander Regueiro <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/REPL.3A.20closure.20vs.20locals.html#182189982">(Nov 29 2019 at 16:32)</a>:</h4>
<p>no problem.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>